﻿// https://mp.weixin.qq.com/s?__biz=MzUxNjY5NTYxNA==&mid=2247489910&idx=2&sn=1e6c0f3a4ed6d675e258920fcebee7fb&scene=21#wechat_redirect
// https://leetcode-cn.com/problems/reverse-string-ii/

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>

using namespace std;

// struct ListNode {
//     int val;
//     ListNode *next;
//     ListNode() : val(0), next(nullptr) {}
//     ListNode(int x) : val(x), next(nullptr) {}
//     ListNode(int x, ListNode *next) : val(x), next(next) {}
// };

class Solution {
public:
    string reverseStr(string s, int k) {
        if (s.size() <= k){
            reverse(s.begin(), s.end());
            return s;
        }
        int n = s.size();
        int m = 0;
        while (n >= k) {
            for (int i = m * 2 * k, j = m * 2 * k + k - 1; i < m * 2 * k + k / 2; i++, j--) {
                swap(s[i], s[j]);
            }
            n -= 2 * k;
            m++;
        }
        for (int i = m * 2 * k, j = s.size() - 1; i < m * 2 * k + n / 2; i++, j--) {
            swap(s[i], s[j]);
        }
        return s;
    }
};

int main() {
    Solution obj = Solution();
}

